export function createErrorCatcher({ browser = true, vue }, fn) {
  if (typeof fn !== 'function') throw new Error('az-error-catcher: Expect a error handling function but missing.')
  if (browser) {
    window.addEventListener('error', (e) => {
      e.errorFrom = 'event error'
      fn(e)
    })
    window.addEventListener('unhandledrejection', function (e) {
      if (e.reason instanceof Error) e = e.reason
      e.errorFrom = 'event unhandledrejection'
      fn(e)
    })
  }
  if (vue && vue.config && Object(vue.config) === vue.config) {
    if (typeof vue.config.errorHandler === 'function') {
      const errHandler = vue.config.errorHandler
      vue.config.errorHandler = function (e) {
        errHandler.call(vue.config, e)
        e.errorFrom = 'Vue'
        fn(e)
      }
      return
    }
    vue.config.errorHandler = function (e) {
      e.errorFrom = 'Vue'
      fn(e)
    }
  }
}
